<template>
  <div class="orderDetailCont" :style="'min-height: '+docheight+'px'">
    <div class="haspadding" v-if="!showTimeOut&&showCont">
      <div class="addressCont" v-if="orderDetail.order_type==2">
        <div class="addressItem">
          <div class="addressImg" v-if="orderDetail.user_name">{{ orderDetail.user_name.charAt(0) }}</div>
          <div class="addressInfo">
            <div class="infoTop">
              <div class="userName">{{ orderDetail.user_name }}</div>
              <div class="userTel">{{ orderDetail.user_phone }}</div>
            </div>
            <div class="infoAddress">{{ orderDetail.delivery_address+orderDetail.house_number }}</div>
          </div>
        </div>
      </div>
      <div class="detailInfo">
        <div class="shopTop">
          <div class="shopNameCont">
            <div class="shopName">订单号：{{ orderDetail.order_number }}</div>
          </div>
          <div class="orderStatus">
              <span class="blueText" v-if="orderDetail.order_status==2">待接单</span>
              <span class="greenText" v-if="orderDetail.order_status==3">已完成</span>
              <span class="blackText" v-if="orderDetail.order_status==4">已关闭</span>
              <span class="blackText" v-if="orderDetail.order_status==5">已退款</span>
          </div>
        </div>
        <div class="goodsList">
          <div class="goodsItem" :key="index" v-for="(item,index) in orderDetail.goods_info">
            <div class="goodsImg">
              <img :src="item.goods_image+ossSrc">
            </div>
            <div class="goodsInfo">
              <div class="goodsName">{{ item.goods_name }}</div>
                <div class="goodsType">
                  <div class="goodsTypeItem">商品数量：x{{ item.num }}</div>
                </div>
                <div class="goodsInfoPrice">
                  <div>商品总价:¥{{ item.goods_total }}</div>
                </div>
            </div>
          </div>
        </div>
        <div class="formCont">
          <div class="formItem">
            <div class="formName">订单类型</div>
            <div class="formType">
              <span v-if="orderDetail.delivery_time=='- -'">邮寄配送</span>
              <span class="goldText" v-if="orderDetail.delivery_time!='- -'">上门服务</span>
            </div>
          </div>
          <div class="formItem">
            <div class="formName">商品总价</div>
            <div>￥{{ orderDetail.goods_total_price }}</div>
          </div>
          <div class="formItem">
            <div class="formName">优惠券</div>
            <div>-¥{{ orderDetail.reduction_price }}</div>
          </div>
          <div class="formItem">
            <div class="formName">实付金额</div>
            <div style="color: #6d94f8">￥{{ orderDetail.order_total_price }}</div>
          </div>
          <div class="formItem">
            <div class="formName">预约时间</div>
            <div>{{ orderDetail.delivery_time }}</div>
          </div>
          <div class="formItem">
            <div class="formName">下单时间</div>
            <div>{{ orderDetail.create_time }}</div>
          </div>
          <div class="formItem" v-if="orderDetail.order_pay_time">
            <div class="formName">支付时间</div>
            <div>{{ orderDetail.order_pay_time }}</div>
          </div>
          <div class="formItem" v-if="orderDetail.order_status==3">
            <div class="formName">完成时间</div>
            <div>{{ orderDetail.complete_time }}</div>
          </div>
          <div class="formItem" v-if="orderDetail.order_status==4">
            <div class="formName">关闭时间</div>
            <div>{{ orderDetail.close_time }}</div>
          </div>
          <div class="formItem" v-if="orderDetail.order_status==5">
            <div class="formName">退款时间</div>
            <div>{{ orderDetail.refund_time }}</div>
          </div>
          <div class="formItem" v-if="orderDetail.order_status==5">
            <div class="formName">退款金额</div>
            <div>¥{{ orderDetail.refund_money }}</div>
          </div>
          <div class="formItem" v-if="orderDetail.order_status==5">
            <div class="formName">退款原因</div>
            <div class="formVal">{{ orderDetail.close_reason }}</div>
          </div>
          <div class="formItem" style="border-bottom: none">
            <div class="formName">订单备注</div>
            <div>{{ orderDetail.service_remark?orderDetail.service_remark:'无备注' }}</div>
          </div>
        </div>
      </div>
      <div class="ctrlCont">
        <div class="orderBtn pinkBtn" v-if="orderDetail.order_status==2" @click="cancelFn">取消订单</div>
        <div class="orderBtn pinkBtn backBtn" v-if="orderDetail.order_status==2" @click="ctrlFn">确认接单({{ orderDetail.delivery_time=='- -'?'邮寄':'上门' }})</div>
      </div>
    </div>
    <van-popup v-model:show="showConfirm" round position="bottom">
      <div class="orderMask">
          <div class="maskTitle">{{ maskTitle }}</div>
          <div class="maskTip">{{ maskTipCont }}</div>
          <div class="maskTip Money">{{ maskTipMoney }}</div>
          <saveBtn color="#6d94f8" style="width: 100%;margin-top: 0.75rem;" :value="maskBtnCont" @saveClick="sureMask"></saveBtn>
      </div>
    </van-popup>
    <toast v-model="showToast" position="middle" width="50%" type="text" :time="1000">{{showText}}</toast>
    <getLoad v-if="!showCont"></getLoad>
    <getTimeOut v-if="showTimeOut"></getTimeOut>
    <sub-load v-if="showLoad"></sub-load>
  </div>
</template>

<script>
import { orderDetails,orderComplete,orderCancel } from '@/http/api'
import {Toast } from 'vux'
import SaveBtn from "@/components/public/saveBtn.vue";

import getLoad from '@/components/public/getLoad'
import getTimeOut from '@/components/public/getTimeOut'
import subLoad from '@/components/public/subLoad.vue'
export default {
  components:{SaveBtn,subLoad,getTimeOut,getLoad,Toast},
  data(){
    return{
      showLoad:false,
      showTimeOut: false,
      showCont:false,
      showText:'',
      showToast:false,
      orderDetail:{},     
      order_id:this.$route.query.id,
      showConfirm:false,
      maskTitle:'',
      maskTipCont:'',
      maskTipMoney:'',
      maskBtnCont:'',
    }
  },
  methods:{
    ctrlFn(){
      this.showConfirm = true
      this.maskBtnCont = '确认接单'
      if(this.orderDetail.delivery_time!='- -'){
        this.maskTitle = '上门服务订单'
        this.maskTipCont = '此订单为上门服务订单，点击确认之后订单状态将转为完成状态。'
      }else{
        this.maskTitle = '邮寄配送订单'
        this.maskTipCont = '此订单为邮寄配送订单，请留意订单的地址，点击确认之后订单状态将转为完成状态'
      }
    },
    sureMask(){
      if(this.maskBtnCont=='确认接单'){
        this.sureFn()
      }else{
        this.cancelApiFn()
      }
    },
    cancelFn(){
      this.showConfirm = true
      this.maskTitle = '驳回订单提示'
      this.maskTipCont = '确认驳回此订单吗？驳回之后金额将原来退回客户账户，订单将关闭。'
      this.maskBtnCont = '确认驳回'
    },
    cancelApiFn(){
      let data ={
        order_id:this.order_id
      }
      this.showConfirm = false
      if(!this.showLoad){
        this.showLoad = true
        orderCancel(data).then((res)=>{
          if(res.data.code==1){
            this.showToast = true
            this.showText = '驳回订单成功'
            this.getData()
          }else{
            this.showToast = true
            this.showText = res.data.msg
          }
          this.showLoad = false
        }).catch(()=>{
          this.showLoad = false
          this.showToast = true
          this.showText = '网络请求超时'
        })
      }
    },
    sureFn(){
      let data ={
        order_id:this.order_id
      }
      this.showConfirm = false
      if(!this.showLoad){
        this.showLoad = true
        orderComplete(data).then((res)=>{
          if(res.data.code==1){
            this.showToast = true
            this.showText = '接单成功'
            this.getData()
          }else{
            this.showToast = true
            this.showText = res.data.msg
          }
          this.showLoad = false
        }).catch(()=>{
          this.showLoad = false
          this.showToast = true
          this.showText = '网络请求超时'
        })
      }
    },
    getData(){
      let data = {
        order_id :this.order_id
      }
      orderDetails(data).then((res)=>{
        if(res.data.code==1){
          this.orderDetail = res.data.data
        }else{
          this.showToast = true
          this.showText = res.data.msg
        }
        this.showCont= true
      }).catch(()=>{
        this.showTimeOut = true
        this.showCont= true
      })
    }
  },
  created() {
    this.getData()
  }
}
</script>

<style scoped lang="less">
.orderDetailCont{
  width: 100%;
  padding-bottom: 3.75rem;
  padding-top: 0.75rem;
  box-sizing: border-box;
  background-color: #edf2ff;
  .addressCont{
    width: 100%;
    padding: 0 4%;
    box-sizing: border-box;
    .addressItem{
      background-color: #fff;
      padding: 0.75rem;
      box-sizing: border-box;
      border-radius: 0.25rem;
      display: flex;
      width: 100%;
      align-items: center;
      margin-bottom: 0.75rem;
      .addressImg{
        width: 2.5rem;
        height: 2.5rem;
        border-radius: 50%;
        text-align: center;
        line-height:2.5rem;
        font-size: 0.9rem;
        background-color: #6d94f8;
        color: white;
        margin-right: 0.75rem;
      }
      .addressInfo{
        width: 0.5rem;
        flex: 1;
        margin-right: 0.75rem;
        .infoTop{
          display: flex;
          align-items: center;
          .userName{
            font-size: 0.7rem;
            font-weight: bold;
          }
          .userTel{
            font-size: 0.6rem;
            color: #999;
            margin: 0 0.5rem;
          }
        }
        .infoAddress{
          width: 100%;
          font-size: 0.6rem;
          line-height: 0.8rem;
          max-height: 1.6rem;
          margin-top: 0.25rem;
          color: #999;
        }
      }
    }
  }
  .verification_code{
    width: 92%;
    margin: 0.75rem 4%;
    padding: 0.75rem;
    box-sizing: border-box;
    background-color: #fff;
    border-radius: 0.25rem;
    // display: flex;
    // align-items: center;
    text-align: center;
    // justify-content: center;
    .codeNum{
      font-size: 1.2rem;
      color: #6d94f8;
      margin-top: 0.25rem;
      font-weight: bold;
    }
  }
  .detailInfo{
    width: 92%;
    background-color: #fff;
    padding: 0.75rem;
    box-sizing: border-box;
    border-radius: 0.25rem;
    margin: 0.75rem auto 0;
    .shopTop{
      display: flex;
      width: 100%;
      align-items: center;
      justify-content: space-between;
      .shopNameCont{
        flex: 1;
        width: 0.5rem;
        display: flex;
        padding: 0.75rem 0;
        align-items: center;
        margin-right: 0.5rem;
        .shopicon{
          font-size: 1.1rem;
          margin-right: 0.25rem;
        }
        .shopName{
          font-size: 0.7rem;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
        .icon-jinrujiantou{
          font-size: 0.8rem;
          position: relative;
        }
      }
      .orderStatus{
        font-size: 0.6rem;
        .redText{
          color: #f9406e;
        }
        .greenText{
          color: #85CE61;
        }
        .blackText{
          color: #666;
        }
        .blueText{
          color: #96A1FE;
        }
      }
    }
    .goodsList{
      width: 100%;
      .goodsItem{
        width: 100%;
        margin-bottom: 1rem;
        display: flex;
        align-items: center;
        .goodsImg{
          width: 4rem;
          height: 4rem;
          overflow: hidden;
          border-radius: 0.25rem;
          margin-right: 0.5rem;
          img{
            width: 100%;
          }
        }
        .goodsInfo{
          width: 0.5rem;
          flex: 1;
          display: flex;
          flex-direction: column;
          height: 4rem;
          justify-content: space-between;
          .goodsName{
            width: 100%;
            line-height: 1rem;
            font-size: 0.7rem;
          }
          .orderTime{
              font-size: 0.6rem;
              color: #666;
          }
          .goodsType{
            display: flex;
            align-items: center;
            justify-content: space-between;
            .goodsNumCont{
              display: flex;
              height: 1rem;
              line-height: 1rem;
              color: #999;
              font-size: 0.6rem;
              align-items: center;
            }
            .goodsTypeItem{
                padding:0.1rem 0.5rem;
                background-color: #edf2ff;
                font-size: 0.6rem;
                height: 1rem;
                line-height: 1rem;
                border-radius: 0.75rem;
                color: #6d94f8;
              }
          }
          .goodsInfoPrice{
            color: #999;
            display: flex;
            justify-content: space-between;
            font-size: 0.6rem;
          }
        }
      }
    }
    .formCont{
      width: 100%;
      .formItem{
        display: flex;
        justify-content: space-between;
        padding: 0.75rem 0;
        align-items: center;
        color: #666;
        font-size: 0.7rem;
        border-bottom: 1px dashed #ccc;
        .formType{
          color: #6d94f8;
          .goldText{
            color: #f99340;
          }
        }
        .formVal{
          width: 1rem;
          text-align: right;
          flex: 1;
          margin-left: 0.75rem;
        }
      }
    }
  }
  .orderDesCont{
    width: 100%;
    padding: 0 4%;
    box-sizing: border-box;
    margin: 1rem 0;
  }
  .formTip{
    width: 100%;
    padding: 0.75rem 4%;
    box-sizing: border-box;
    color: #666;
    font-size: 0.6rem;
    .reportBtn{
      text-decoration: underline;
      color: #6d94f8;
    }
  }
  .ctrlCont{
    width: 100%;
    padding: 0.5rem 4%;
    box-sizing: border-box;
    position: fixed;
    bottom: 0;
    left: 0;
    background-color: #fff;
    display: flex;
    justify-content: flex-end;
    text-align: center;
    .orderBtn{
      padding: 0.35rem 0.75rem;
      text-align: center;
      border-radius: 2rem;
      font-size: 0.7rem;
      box-sizing: border-box;
      margin-left: 0.5rem;
    }
    .pinkBtn{
      border: 1px solid #6d94f8;
      color: #6d94f8;
    }
    .greenBtn{
      border: 1px solid #6d94f8;
      color:#6d94f8;
    }
    .backBtn{
      background-color: #6d94f8;
      color: #fff;
    }
  }
  .maskAddress{
    width: 100%;
    padding: 0.75rem 4%;
    box-sizing: border-box;
    .addressItem{
      background-color: #fff;
      padding: 0.5rem;
      box-sizing: border-box;
      border-radius: 0.25rem;
      display: flex;
      width: 100%;
      align-items: center;
      .addressImg{
        width: 2rem;
        height: 2rem;
        border-radius: 50%;
        text-align: center;
        line-height:2rem;
        font-size: 0.8rem;
        background-color: #61CCD3;
        color: white;
        margin-right: 0.75rem;
      }
      .addressInfo{
        width: 0.5rem;
        flex: 1;
        margin-right: 0.75rem;
        .infoTop{
          display: flex;
          align-items: center;
          .userName{
            font-size: 0.7rem;
            font-weight: bold;
          }
          .userTel{
            font-size: 0.6rem;
            color: #999;
            margin: 0 0.5rem;
          }
        }
        .infoAddress{
          width: 100%;
          font-size: 0.6rem;
          line-height: 0.8rem;
          max-height: 1.6rem;
          margin-top: 0.25rem;
          color: #999;
        }
      }
      .checkAddress{
        font-size: 0.7rem;
        color: white;
        // color: #61CCD3;
        width: 0.9rem;
        line-height: 0.9rem;
        height: 0.9rem;
        border: 1px solid #61CCD3;
        text-align: center;
        border-radius: 50%;
      }
      .checkAddressActive{
        background-color: #61CCD3;
      }
    }
  }
  .orderMask{
  padding: 0.75rem;
  box-sizing: border-box;
  .maskTitle{
    font-size: 0.8rem;
    font-weight: bold;
    text-align: center;
  }
  .maskTip{
    margin-top: 0.5rem;
    line-height: 1.1rem;
  }
}
}
.haspadding{
  padding-bottom: 1rem;
}
</style>
